package com.distance;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

import com.bookmarks.BookmarksAdapter;
import com.google.android.maps.GeoPoint;
import com.kmlparser.ReverseGeoCoder;
import com.route.DetailedLocation;
import com.route.Route;

public class Destination extends Activity {
    Button button;
    Button curLocation;
    EditText input;
    ListView bookmarks;
    ArrayAdapter<String> mAdapter;
    DetailedLocation searchResult;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.destination);
	
	setTitle("Distance - Set Journey End Point");
	button = (Button)findViewById(R.id.Button01);
	curLocation = (Button)findViewById(R.id.Button02);
	input = (EditText)findViewById(R.id.DestinationET01);
	bookmarks = (ListView)findViewById(R.id.DestinationLV01);
	mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Bookmarks.locationStrings);
	bookmarks.setAdapter(mAdapter);
	this.bookmarks.setOnItemClickListener(new OnItemClickListener(){

	    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
		    long arg3) {
		//Toast.makeText(Destination.this, "test "+arg2, Toast.LENGTH_SHORT).show();
		Route.end = Bookmarks.locationList.get(arg2);
		Intent intent = new Intent();
		intent.setClassName("com.distance", "com.distance.Distance");
		startActivity(intent);
	    }

	});
	this.curLocation.setOnClickListener(new OnClickListener(){

	    public void onClick(View v) {
		if(Route.currentLocation!=null){
		    searchResult = null;
		    searchResult = ReverseGeoCoder.reverseGeoCode(((Double)(Route.currentLocation.getLatitudeE6()/1E6)).toString(), ((Double)(Route.currentLocation.getLongitudeE6()/1E6)).toString());
		    if(searchResult!=null){
			input.setText(null);
			Bookmarks.locationList.add(searchResult);
			Bookmarks.locationStrings.add(searchResult.getLocationName());
			BookmarksAdapter.saveBookmarkList(Bookmarks.locationList);
			//mAdapter.add(null);
			mAdapter.notifyDataSetChanged();
			//Toast.makeText(Destination.this, searchResult.getLocationName(), Toast.LENGTH_SHORT).show();
		    }
		}else Toast.makeText(Destination.this, "Please get your current location.", Toast.LENGTH_SHORT).show();


	    }

	});
	this.button.setOnClickListener(new OnClickListener(){

	    public void onClick(View v) {
		if(!input.getText().toString().equals("")){
		    searchResult = null;
		    searchResult = performGeoCode(input.getText().toString(),true);
		    if(searchResult!=null){
			input.setText(null);
			Bookmarks.locationList.add(searchResult);
			Bookmarks.locationStrings.add(searchResult.getLocationName());
			BookmarksAdapter.saveBookmarkList(Bookmarks.locationList);
			//mAdapter.add(null);
			mAdapter.notifyDataSetChanged();
		    }
		}else Toast.makeText(Destination.this, "Cannot search for empty location.", Toast.LENGTH_SHORT).show();


	    }

	});
	//bookmarks.


	//android.R.layout.simple_list_item_1
	// TODO Auto-generated method stub
    }

    private DetailedLocation performGeoCode(String in, boolean isAddr){
	Geocoder geocoder = new Geocoder(this);
	if(isAddr){
	    try{
		List<Address> addresses = geocoder.getFromLocationName(in, 1);

		if(addresses!=null){

		    Toast.makeText(Destination.this, Double.toString(addresses.get(0).getLatitude())+ ", "+Double.toString(addresses.get(0).getLongitude()), Toast.LENGTH_SHORT).show();
		    return new DetailedLocation(addresses.get(0).getAddressLine(0),new GeoPoint((int)(addresses.get(0).getLatitude()*1E6), (int)(addresses.get(0).getLongitude()*1E6)),"Enter a detailed description");
		}else return null;
	    } catch(Exception e){
		return null;
	    }
	}
	return null;

    }

}
